/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frreport_params.c                                  */
/*  NOTE      =                                                    */
/*  DATE      = 2015/03/03 by guojq                                */
/*******************************************************************/
#include <stdio.h>

#include "frcomm.h"
#include "frlog.h"
#include "frxml_parse.h"
#include "frmem.h"

#include "frreport_params.h"

Report_params st_report_params;

/*******************************************************************************
*  FUNC     :  获取配置信息
*  ARGS     :
*  RTN      :  FR_SUCC:成功, FR_FAIL:失败
*  NOTE     :
*******************************************************************************/
int frreport_params_load(const char *p_filename)
{
    xmlDocPtr p_st_doc = NULL;
    int i_ret_fun=0, i_value=0;
    const char *p_xpath=NULL, *p_key=NULL;
    char *p_value=NULL;

    p_st_doc = frxml_open_file((char *)p_filename);
    if (p_st_doc == NULL)
    {
        FRLOG_ERROR_DETAIL("config file err[%s]", p_filename);
        return FR_FAIL;
    }

    p_xpath = "/report_set";
    p_key = "agent_id";
    p_value = NULL;
    i_ret_fun = frxml_get2ptr_string(p_st_doc, p_xpath, p_key, &p_value);
    if(p_value )
    {
        snprintf(st_report_params.arr_agent_from, sizeof(st_report_params.arr_agent_from), "%s", p_value);
        frfree(p_value);
        p_value=NULL;
    }
    else
        st_report_params.arr_agent_from[0]='\0';

    p_key = "report_file";
    i_ret_fun = frxml_get2ptr_string(p_st_doc, p_xpath, p_key, &p_value);
    if(p_value )
    {
        snprintf(st_report_params.arr_report_file, sizeof(st_report_params.arr_report_file), "%s", p_value);
        frfree(p_value);
        p_value=NULL;
    }
    else
        st_report_params.arr_report_file[0]='\0';

    p_key = "report_ip";
    i_ret_fun = frxml_get2ptr_string(p_st_doc, p_xpath, p_key, &p_value);
    if(p_value )
    {
        snprintf(st_report_params.arr_report_ip, sizeof(st_report_params.arr_report_ip), "%s", p_value);
        frfree(p_value);
        p_value=NULL;
    }
    else
        st_report_params.arr_report_ip[0]='\0';
    p_key = "report_port";
    i_value = 0;
    i_ret_fun = frxml_get2ptr_int(p_st_doc, p_xpath, p_key, &i_value);
    if(i_ret_fun == FR_SUCC && i_value > 0)
        st_report_params.ui_report_port = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", p_filename, p_key);
        st_report_params.ui_report_port = 0;
    }

    p_key = "report_max";
    i_value = 0;
    i_ret_fun = frxml_get2ptr_int(p_st_doc, p_xpath, p_key, &i_value);
    if(i_ret_fun == FR_SUCC && i_value > 0)
        st_report_params.ui_report_max = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", p_filename, p_key);
        st_report_params.ui_report_max = 0;
    }

    p_key = "base64_file";
    i_ret_fun = frxml_get2ptr_string(p_st_doc, p_xpath, p_key, &p_value);
    if(p_value )
    {
        snprintf(st_report_params.arr_base64_file, sizeof(st_report_params.arr_base64_file), "%s", p_value);
        frfree(p_value);
        p_value=NULL;
    }
    else
        snprintf(st_report_params.arr_base64_file, sizeof(st_report_params.arr_base64_file), "../logs/report_base64.log");

    p_key = "file_maxsize";
    i_ret_fun = frxml_get2ptr_int(p_st_doc, p_xpath, p_key, &i_value);
    if(i_ret_fun == FR_SUCC && i_value > 0)
        st_report_params.ui_file_maxsize = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", p_filename, p_key);
        st_report_params.ui_file_maxsize = 10240;
    }

    frxml_close_file(p_st_doc);

    return FR_SUCC;
}
